use lz4_flex::{compress_prepend_size, decompress_size_prepended};
use std::fs::File;
use std::io::{self, Read, Write};

fn main() -> io::Result<()> {
    // 读取test.md文件
    let mut input_file = File::open("./test.md")?;
    let mut input_data = Vec::new();
    input_file.read_to_end(&mut input_data)?;

    // 压缩数据
    let compressed_data = compress_prepend_size(&input_data);

    // 将压缩数据写入test.md.lz4
    let mut compressed_file = File::create("./test.md.lz4")?;
    compressed_file.write_all(&compressed_data)?;

    // 读取压缩文件
    let mut compressed_file = File::open("./test.md.lz4")?;
    let mut compressed_data = Vec::new();
    compressed_file.read_to_end(&mut compressed_data)?;

    // 解压缩数据
    let decompressed_data = decompress_size_prepended(&compressed_data)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

    // 将解压缩数据写入output.md
    let mut output_file = File::create("./output.md")?;
    output_file.write_all(&decompressed_data)?;

    Ok(())
}
